package com.migo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.migo.dao.TutorialVideoDao;
import com.migo.defence.RtCode;
import com.migo.entity.TutorialVideoEntity;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.migo.dao.MgSampleVideoDao;
import com.migo.entity.MgSampleVideoEntity;
import com.migo.service.MgSampleVideoService;

import javax.annotation.Resource;


@Service("mgSampleVideoService")
public class MgSampleVideoServiceImpl implements MgSampleVideoService {

	private int pagesize = 5;

	@Autowired
	private MgSampleVideoDao mgSampleVideoDao;

	@Resource
	private RedisCacheUtil<List<MgSampleVideoEntity>> redisCacheUtil;

	@Autowired
	TutorialVideoDao tutorialVideoDao;


	@Override
	public MgSampleVideoEntity queryObject(Integer id){
		return mgSampleVideoDao.queryObject(id);
	}
	
	@Override
	public List<MgSampleVideoEntity> queryList(Map<String, Object> map){
		return mgSampleVideoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return mgSampleVideoDao.queryTotal(map);
	}
	
	@Override
	public void save(MgSampleVideoEntity mgSampleVideo){
		mgSampleVideoDao.save(mgSampleVideo);
	}
	
	@Override
	public void update(MgSampleVideoEntity mgSampleVideo){
		mgSampleVideoDao.update(mgSampleVideo);
	}
	
	@Override
	public void delete(Integer id){
		mgSampleVideoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		mgSampleVideoDao.deleteBatch(ids);
	}

	@Override
	public List<MgSampleVideoEntity> querySampleVideoList(Map<String, Object> map) {
		return mgSampleVideoDao.querySampleVideoList(map);
	}

	@Override
	public boolean flushHomeVideoCache() {
		String[] language_array = {"zh","en","ja"};
		for(String language :language_array){
			String key = "queryHomeVideo_" + language;
			List<MgSampleVideoEntity> mgSampleVideoList = null;
			if (redisCacheUtil.hashKey(key)) {
				mgSampleVideoList = redisCacheUtil.getCacheList(key);
				mgSampleVideoList.sort(Comparator.comparing(MgSampleVideoEntity::getSort));

				redisCacheUtil.deleteObject(key);

			} else {
				//查询数据库写入redis
				synchronized (this) {
					Map<String, Object> params = new HashMap<>();
					params.put("language",language);
					mgSampleVideoList = querySampleVideoList(params);
					redisCacheUtil.setCacheList(key, mgSampleVideoList);
				}
			}
		}

		return  true;
	}

	@Override
	public List<Map<String,Object>> queryHomePage(Map<String, Object> params) {
		if (!params.containsKey("language")) {
			params.put("language", "en");
		}
		List<Map<String,Object>> data = null;
		String key = "queryHomeVideo:" + params.get("language")+":"+params.get("proid");
		if (redisCacheUtil.hashKey(key)) {
			data = redisCacheUtil.getCacheList(key);
			if (params.containsKey("flush")) {  //刷新标志
				redisCacheUtil.deleteObject(key);
			}
		} else {
			//查询数据库写入redis
			synchronized (this) {
				//查询产品分栏
				List<Map<String, Object>> videoData = tutorialVideoDao.queryVideoDataByParams(params);
				if (videoData == null || videoData.size() == 0) {
					redisCacheUtil.setCacheList(key, videoData);
					return null;
				}
				List<MgSampleVideoEntity> mgSampleVideoEntities = querySampleVideoList(params);
				if (mgSampleVideoEntities == null || mgSampleVideoEntities.size() == 0) {
					redisCacheUtil.setCacheList(key, videoData);
					return null;
				}
				for (int i = 0; i < videoData.size(); i++) {
					Map map = videoData.get(i);
					int typeid = Integer.parseInt(map.get("typeId").toString());
					List<MgSampleVideoEntity> entitys = new ArrayList<>();
					for (MgSampleVideoEntity mgSampleVideoEntity : mgSampleVideoEntities) {
						if (typeid == mgSampleVideoEntity.getColumnstypeId()) {
							entitys.add(mgSampleVideoEntity);
						}
//						if(entitys.size()==pagesize){
//							break;
//						}
					}
					map.put("videoEntitys", entitys);
					videoData.set(i, map);
				}
				redisCacheUtil.setCacheListBySort(key, videoData);
				data = videoData;
			}
		}
		return data;
	}

	@Override
	public MgSampleVideoEntity queryTutorialVideoDetail(Map<String, Object> params) {
		MgSampleVideoEntity result = null;
		//缓存处理开始
		String id = params.get("id").toString();
		String key = "queryTutorialVideo:Detail:" + id;
		boolean exisKey = redisCacheUtil.hashKey(key);
		if (exisKey) {
			result = redisCacheUtil.getCacheObject(key);
			if (params.containsKey("flush")) {  //刷新标志
				redisCacheUtil.deleteObject(key);
			}
		} else {
			result = queryObject(Integer.parseInt(id));
			//查询视频详细信息
			ArrayList<Map<String,Object>> list = tutorialVideoDao.tutorialVideoDetail(Integer.parseInt(id));
			result.setVideoDetails(list);
			redisCacheUtil.setCacheObject(key,result);
		}
		return result;
	}

	@Override
	public List<MgSampleVideoEntity> queryTutorialVideoByPage(Map<String, Object> params) {
		List<MgSampleVideoEntity> result = null;
		if (!params.containsKey("language")) {
			params.put("language", "en");
		}
		if (!params.containsKey("columnstypeid")) {
			params.put("columnstypeid", 1);
		}
		if (!params.containsKey("page")) {
			params.put("page", 1);
		}
		if(Integer.parseInt(params.get("page").toString())<1){
			params.put("page", 1);
		}
		if (!params.containsKey("size")) {
			params.put("size", pagesize);
		}
		params.put("page", (Integer.parseInt(params.get("page").toString())-1)*pagesize);
		String key = "queryTutorialVideoByPage:" + params.get("language")
				+":"+params.get("columnstypeid")+":"+params.get("page")+":"+params.get("size");
		boolean exisKey = redisCacheUtil.hashKey(key);
		if(exisKey){
			result = redisCacheUtil.getCacheObject(key);
			if (params.containsKey("flush")) {  //刷新标志
				redisCacheUtil.deleteObject(key);
			}
		}else{
			result = mgSampleVideoDao.queryTutorialVideoByPage(params);
			redisCacheUtil.setCacheObject(key,result);
		}
		return result;
	}
}
